package com.shop2cn.iapi.sqidcard.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqidcard.parameter.SaveIdcardReq;
import com.shop2cn.iapi.sqidcard.service.SaveIdcardCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.model.sqidcard.IdcardInfo;
import com.ymttest.database.sqlwapper.sqidcard.IdcardInfoWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_SaveIdcard {
	
	//java接口
	private static SaveIdcardReq saveIdcardReq;
	private static SaveIdcardCall saveIdcardCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("保存订购人身份证号接口");
	}

	@Before
	public void caseUp() {
		saveIdcardReq = new SaveIdcardReq();
		saveIdcardCall = new SaveIdcardCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveIdcardCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(Long idcardId) throws Exception {
		
		String newKey = "123456789";
		IdcardInfoWrapper idcardInfoWrapper = new IdcardInfoWrapper();

		IdcardInfo actualResult = idcardInfoWrapper
				.selectByPrimaryKey(idcardId);

		Logger.verifyEquals(saveIdcardReq.getIdcardName(),
				actualResult.getIdcard_name(), "验证Idcard_name");
		Logger.verifyEquals(
				CipherUtil.encryptAES(newKey, saveIdcardReq.getIdcardNo()),
				actualResult.getIdcard_no(), "验证Idcard_no");
		Logger.verifyEquals(saveIdcardReq.getUserId(),
				actualResult.getUser_id(), "验证User_id");
		Logger.verifyEquals(Utils.falseIfNull(saveIdcardReq.getPushStatus()),
				actualResult.getPush_status(), "验证Push_status");
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_001() {
		Logger.start(true, "保存身份证信息成功");
		
		try {
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
			saveIdcardReq.setUserId(TestDataManager.getRandomIntNum(8) * 10000L);
			saveIdcardReq.setPushStatus(true);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(true, saveIdcardCall.getSuccess(), "验证success");
			
			verifyDb(saveIdcardCall.getIdcardId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_002() {
		Logger.start(true, "同一个userId, 保存多个身份证信息成功");
		
		try {
			
			Long userId = TestDataManager.getRandomIntNum(8) * 10000L;
			String name = TestDataManager.getChineseName();
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			saveIdcardReq.setIdcardName(name);
			saveIdcardReq.setUserId(userId);
			saveIdcardReq.setPushStatus(true);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(true, saveIdcardCall.getSuccess(), "验证success");
			
			Thread.sleep(5000);
			
			verifyDb(saveIdcardCall.getIdcardId());
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
			saveIdcardReq.setUserId(userId);
			saveIdcardReq.setPushStatus(true);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(true, saveIdcardCall.getSuccess(), "验证success");
			
			verifyDb(saveIdcardCall.getIdcardId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_003() {
		Logger.start(true, "同一个userId的身份信息，先保存pushStatus为false，再次保存pushStatus为true");
		
		try {
			
			Long userId = TestDataManager.getRandomIntNum(8) * 10000L;
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
			saveIdcardReq.setUserId(userId);
			saveIdcardReq.setPushStatus(false);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			saveIdcardReq.setPushStatus(true);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(true, saveIdcardCall.getSuccess(), "验证success");
			
			verifyDb(saveIdcardCall.getIdcardId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_004() {
		Logger.start(true, "不输入usesrId,保存失败");
		
		try {
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
			//saveIdcardReq.setUserId(null);
			saveIdcardReq.setPushStatus(false);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(false, saveIdcardCall.getSuccess(), "验证success");
			Logger.verifyEquals("userId:must be greater than or equal to 1",
					saveIdcardCall.getMsg(), "验证errorMessage");
			Logger.verifyEquals(100, saveIdcardCall.getErrorCode(),
					"验证errorCode");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_005() {
		Logger.start(true, "不输入idcadNo，保存失败");
		
		try {
			
			//saveIdcardReq.setIdcardNo(null);
			saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
			saveIdcardReq.setUserId(TestDataManager.getRandomIntNum(8) * 10000L);
			saveIdcardReq.setPushStatus(true);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(false, saveIdcardCall.getSuccess(), "验证success");
			Logger.verifyEquals("idcardNo:may not be empty",
					saveIdcardCall.getMsg(), "验证errorMessage");
			Logger.verifyEquals(100, saveIdcardCall.getErrorCode(),
					"验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_006() {
		Logger.start(true, "不输入idcardName， 保存失败");
		
		try {
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			//saveIdcardReq.setIdcardName(null);
			saveIdcardReq.setUserId(TestDataManager.getRandomIntNum(8) * 10000L);
			saveIdcardReq.setPushStatus(true);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(false, saveIdcardCall.getSuccess(), "验证success");
			Logger.verifyEquals("idcardName:may not be empty",
					saveIdcardCall.getMsg(), "验证errorMessage");
			Logger.verifyEquals(100, saveIdcardCall.getErrorCode(),
					"验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveIdcard_007() {
		Logger.start(true, "不输入pushStatus，保存成功");
		
		try {
			
			saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
			saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
			saveIdcardReq.setUserId(TestDataManager.getRandomIntNum(8) * 10000L);
			//saveIdcardReq.setPushStatus(null);
			
			saveIdcardCall.setData(saveIdcardReq);
			saveIdcardCall.callService();
			
			Logger.verifyEquals(true, saveIdcardCall.getSuccess(), "验证success");
			
			verifyDb(saveIdcardCall.getIdcardId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
